package com.huike.game.Activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.huike.game.R;
import com.huike.game.base.Game18;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivityDesc65 extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private ImageView iv_game65_video;
    private LinearLayout ll_game65_layout;
    private VideoView vv_game65;
    private ImageView iv_game65_image;
    private Boolean isClick = false;//图片大小开关
    private Game18 bean;
    private TextView tv_game65desc_way;
    private TextView tv_game65desc_title;
    private ImageView iv_game65_left;
    private ImageView iv_game65_right;
    private TextView tv_game65desc_reg;
    private TextView tv_game65desc_fine;
    private TextView tv_game65desc_score;
    private int i = 0;//显示数据下标
    private ArrayList<Game18.DataBean.DescBean> beans;
    private int size;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_desc65);
        beans = new ArrayList<>();
        EventBus.getDefault().register(this);

        initView();
        initData();
        iniClick();
    }

    private void initData() {
        //获取违章条数
        size = bean.getData().getDesc().size();
        for (int j = 0; j < size; j++) {
            beans.add(bean.getData().getDesc().get(j));
        }

        tv_game65desc_title.setText(beans.get(i).getViolationDesc());
        tv_game65desc_reg.setText("违章：" + size);
        tv_game65desc_fine.setText("罚款："+beans.get(i).getPrice());
        tv_game65desc_score.setText("扣分："+beans.get(i).getScore());

    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void EventbusMessage(Game18 bean) {
        this.bean = bean;
    }

    private void iniClick() {

        //视频图片被点击
        iv_game65_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ll_game65_layout.setVisibility(View.GONE);
                vv_game65.setVisibility(View.VISIBLE);

                //播放视频
                vv_game65.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.ren);
                vv_game65.setMediaController(new MediaController(GameActivityDesc65.this));
                vv_game65.start();


            }
        });

        //监听视频播放完成 隐藏播放视图
        vv_game65.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                ll_game65_layout.setVisibility(View.VISIBLE);
                vv_game65.setVisibility(View.GONE);
            }
        });

        //图片点击
        iv_game65_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewGroup.LayoutParams params = iv_game65_image.getLayoutParams();
                //通过测量得高图片原生宽高
                iv_game65_image.measure(0, 0);
                int width = iv_game65_image.getMeasuredWidth();
                int height = iv_game65_image.getMeasuredHeight();

                isClick = !isClick;

                if (isClick) {
//                    isClick = true;//变大
                    params.width = 700;
                    params.height = 300;
                    iv_game65_image.setLayoutParams(params);

                } else {
//                    isClick = false;//还原
                    params.width = width;
                    params.height = height;
                    iv_game65_image.setLayoutParams(params);

                }


            }
        });

        //点击上一个
        iv_game65_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(size == 1){
                    Toast.makeText(GameActivityDesc65.this, "只有一条违章记录哦！", Toast.LENGTH_SHORT).show();
                    return;
                }
                i = i-1;

                if(i == -1){
                    i = size - 1;
                }
                Log.e("---i",i +"");
                Log.e("---",beans.get(i).getViolationDesc());
                //重新赐值
                tv_game65desc_title.setText(beans.get(i).getViolationDesc());
                tv_game65desc_reg.setText("违章：" + size);
                tv_game65desc_fine.setText("罚款："+beans.get(i).getPrice());
                tv_game65desc_score.setText("扣分："+beans.get(i).getScore());

            }
        });

        //点击下一个
        iv_game65_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(size == 1){
                    Toast.makeText(GameActivityDesc65.this, "只有一条违章记录哦！", Toast.LENGTH_SHORT).show();
                    return;
                }

                    i = i +1;

                    if(i == size  ){
                        i = 0;
                    }

                //重新赐值
                tv_game65desc_title.setText(beans.get(i).getViolationDesc());
                tv_game65desc_reg.setText("违章：" + size);
                tv_game65desc_fine.setText("罚款："+beans.get(i).getPrice());
                tv_game65desc_score.setText("扣分："+beans.get(i).getScore());
            }
        });


    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);

        iv_game65_video = (ImageView) findViewById(R.id.iv_game65_video);
        ll_game65_layout = (LinearLayout) findViewById(R.id.ll_game65_layout);
        vv_game65 = (VideoView) findViewById(R.id.vv_game65);
        iv_game65_image = findViewById(R.id.iv_game65_image);
        tv_game65desc_way = (TextView) findViewById(R.id.tv_game65desc_way);
        tv_game65desc_title = (TextView) findViewById(R.id.tv_game65desc_title);
        iv_game65_left = (ImageView) findViewById(R.id.iv_game65_left);
        iv_game65_right = (ImageView) findViewById(R.id.iv_game65_right);

        iv_rigth_image.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.tiao);
        tv_content_title.setText("查询结果");
        tv_left_text.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);


        tv_game65desc_reg = (TextView) findViewById(R.id.tv_game65desc_reg);
        tv_game65desc_fine = (TextView) findViewById(R.id.tv_game65desc_fine);
        tv_game65desc_score = (TextView) findViewById(R.id.tv_game65desc_score);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
